(function (window, $, undefined) {
    var Base = {};
    Base.ajax = {};
    Base.util = {};

    /**
   * jquery ajax请求
   */
    Base.ajax.request = function (options) {
        options.url = '/api' + options.url;

        $.extend(options, {
            headers: {
                "Context-type": "application/x-www-form-urlencoded",
                "Accept": "application/json"
            },
            dataType: "json",
            cache: false,
            success: function (res, status) {
                if (res.error) {
                    if (typeof options.warn === 'function') {
                        options.warn.call(this, res.error);
                    } else {
                        //something
                    }
                } else {
                    if (typeof options.cb === 'function') {
                        options.cb.call(this, res.data);
                    }
                }
            },
            error: function (xhr, status, err) {
                console.log(err);
            }
        })

        $.ajax(options);
    }

    /**
     * ajax get请求
     */
    Base.ajax.get = function (options) {
        options.type = 'GET';
        this.request(options);
    }

    /**
     * ajax post请求
     */
    Base.ajax.post = function (options) {
        options.type = 'POST';
        this.request(options);
    }


    /**
     * 加载layui模块
     */
    Base.util.loadLayuiModel = function (model, cb) {
        var layui = window.layui;
        layui.use(model, function () {
            var object = {};
            if (Object.prototype.toString.call(model) === '[object Array]') {
                for (let k of model) {
                    object[k] = layui[k];
                }
            } else {
                object[model] = layui[model];
            }
            cb.call(this, object);
        });
    }


    /**
     * 加载script
     */
    Base.util.loadJavaScript = function (src, cb) {
        var script = document.createElement('script');
        script.charset = 'utf-8';
        script.type = 'text/javascript';
        script.src = src;
        script.defer = 'defer';
        document.getElementsByTagName('body')[0].after(script);
        if (window.ActiveXObject) {
            script.onreadystatechange = function () {
                if (this.readyState === 'loaded' || this.readyState === 'complete') {
                    if (typeof cb === 'function') {
                        cb();
                    }
                }
            }
        } else {
            script.onload = function () {
                if (typeof cb === 'function') {
                    cb();
                }
            }
        }
    }





    window.Base = Base;

})(window, $);